<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="售后单号" prop="afterSaleNumber">
              <el-input v-model="queryParams.afterSaleNumber" placeholder="请输入售后单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="内部订单号" prop="innerOrderNumber">
              <el-input v-model="queryParams.innerOrderNumber" placeholder="请输入内部订单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="售后分类" prop="afterSaleCategory">
              <el-input v-model="queryParams.afterSaleCategory" placeholder="请输入售后分类" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="线上申请金额" prop="onlineApplyMoney">
              <el-input v-model="queryParams.onlineApplyMoney" placeholder="请输入线上申请金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="线上退运费" prop="onlineQuitFreight">
              <el-input v-model="queryParams.onlineQuitFreight" placeholder="请输入线上退运费" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="优惠或者差额" prop="discountOrDifference">
              <el-input v-model="queryParams.discountOrDifference" placeholder="请输入优惠或者差额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="卖家应退金额" prop="sellerShouldQuitMoney">
              <el-input v-model="queryParams.sellerShouldQuitMoney" placeholder="请输入卖家应退金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="买家应补金额" prop="buyerShouldSupplementMoney">
              <el-input v-model="queryParams.buyerShouldSupplementMoney" placeholder="请输入买家应补金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="卖家应退运费" prop="sellerShouldQuitFreight">
              <el-input v-model="queryParams.sellerShouldQuitFreight" placeholder="请输入卖家应退运费" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="实际应退金额" prop="actualQuitMoney">
              <el-input v-model="queryParams.actualQuitMoney" placeholder="请输入实际应退金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="店铺编号" prop="shopNumber">
              <el-input v-model="queryParams.shopNumber" placeholder="请输入店铺编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="店铺名称" prop="shopName">
              <el-input v-model="queryParams.shopName" placeholder="请输入店铺名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="商品编码" prop="productNumber">
              <el-input v-model="queryParams.productNumber" placeholder="请输入商品编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="金蝶的物料编码" prop="jdMaterialCode">
              <el-input v-model="queryParams.jdMaterialCode" placeholder="请输入金蝶的物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="款式编码" prop="styleNumber">
              <el-input v-model="queryParams.styleNumber" placeholder="请输入款式编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请数量" prop="applyNumber">
              <el-input v-model="queryParams.applyNumber" placeholder="请输入申请数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="单价" prop="unitPrice">
              <el-input v-model="queryParams.unitPrice" placeholder="请输入单价" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请金额" prop="applyPrice">
              <el-input v-model="queryParams.applyPrice" placeholder="请输入申请金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="颜色规格" prop="colorSku">
              <el-input v-model="queryParams.colorSku" placeholder="请输入颜色规格" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="原订单下单金额" prop="originalOrderPrice">
              <el-input v-model="queryParams.originalOrderPrice" placeholder="请输入原订单下单金额" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="最后确认时间（用于时间区段搜索）" prop="newOrderDate">
              <el-date-picker
                clearable
                v-model="dateRange"
                type="daterange"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择最后确认时间"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="最后确认时间" prop="lastConfirmationTime">
              <el-input v-model="form.lastConfirmationTime" placeholder="请输入最后确认时间" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['twoCSale:jstAfterSaleOrder:add']">新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['twoCSale:jstAfterSaleOrder:edit']"
              >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['twoCSale:jstAfterSaleOrder:remove']"
              >删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['twoCSale:jstAfterSaleOrder:export']">导出 </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="jstAfterSaleOrderList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" v-if="true" />
        <el-table-column label="售后单号" align="center" prop="afterSaleNumber" />
        <el-table-column label="内部订单号" align="center" prop="innerOrderNumber" />
        <el-table-column label="线上类型" align="center" prop="onlineType" />
        <el-table-column label="售后分类" align="center" prop="afterSaleCategory" />
        <el-table-column label="线上申请金额" align="center" prop="onlineApplyMoney" />
        <el-table-column label="线上退运费" align="center" prop="onlineQuitFreight" />
        <el-table-column label="优惠或者差额" align="center" prop="discountOrDifference" />
        <el-table-column label="卖家应退金额" align="center" prop="sellerShouldQuitMoney" />
        <el-table-column label="买家应补金额" align="center" prop="buyerShouldSupplementMoney" />
        <el-table-column label="卖家应退运费" align="center" prop="sellerShouldQuitFreight" />
        <el-table-column label="实际应退金额" align="center" prop="actualQuitMoney" />
        <el-table-column label="店铺编号" align="center" prop="shopNumber" />
        <el-table-column label="店铺名称" align="center" prop="shopName" />
        <el-table-column label="原订单类型" align="center" prop="originalOrderType" />
        <el-table-column label="售后状态" align="center" prop="afterSaleStatus" />
        <el-table-column label="商品编码" align="center" prop="productNumber" />
        <el-table-column label="金蝶的物料编码" align="center" prop="jdMaterialCode" />
        <el-table-column label="款式编码" align="center" prop="styleNumber" />
        <el-table-column label="申请数量" align="center" prop="applyNumber" />
        <el-table-column label="单价" align="center" prop="unitPrice" />
        <el-table-column label="申请金额" align="center" prop="applyPrice" />
        <el-table-column label="售后单类型" align="center" prop="afterSaleType" />
        <el-table-column label="颜色规格" align="center" prop="colorSku" />
        <el-table-column label="原订单下单金额" align="center" prop="originalOrderPrice" />
        <el-table-column label="最后确认时间（用于时间区段搜索）" align="center" prop="newOrderDate" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.newOrderDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最后确认时间" align="center" prop="lastConfirmationTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['twoCSale:jstAfterSaleOrder:edit']"
              ></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['twoCSale:jstAfterSaleOrder:remove']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改聚水潭-售后单对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="jstAfterSaleOrderFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="售后单号" prop="afterSaleNumber">
          <el-input v-model="form.afterSaleNumber" placeholder="请输入售后单号" />
        </el-form-item>
        <el-form-item label="内部订单号" prop="innerOrderNumber">
          <el-input v-model="form.innerOrderNumber" placeholder="请输入内部订单号" />
        </el-form-item>
        <el-form-item label="售后分类" prop="afterSaleCategory">
          <el-input v-model="form.afterSaleCategory" placeholder="请输入售后分类" />
        </el-form-item>
        <el-form-item label="线上申请金额" prop="onlineApplyMoney">
          <el-input v-model="form.onlineApplyMoney" placeholder="请输入线上申请金额" />
        </el-form-item>
        <el-form-item label="线上退运费" prop="onlineQuitFreight">
          <el-input v-model="form.onlineQuitFreight" placeholder="请输入线上退运费" />
        </el-form-item>
        <el-form-item label="优惠或者差额" prop="discountOrDifference">
          <el-input v-model="form.discountOrDifference" placeholder="请输入优惠或者差额" />
        </el-form-item>
        <el-form-item label="卖家应退金额" prop="sellerShouldQuitMoney">
          <el-input v-model="form.sellerShouldQuitMoney" placeholder="请输入卖家应退金额" />
        </el-form-item>
        <el-form-item label="买家应补金额" prop="buyerShouldSupplementMoney">
          <el-input v-model="form.buyerShouldSupplementMoney" placeholder="请输入买家应补金额" />
        </el-form-item>
        <el-form-item label="卖家应退运费" prop="sellerShouldQuitFreight">
          <el-input v-model="form.sellerShouldQuitFreight" placeholder="请输入卖家应退运费" />
        </el-form-item>
        <el-form-item label="实际应退金额" prop="actualQuitMoney">
          <el-input v-model="form.actualQuitMoney" placeholder="请输入实际应退金额" />
        </el-form-item>
        <el-form-item label="店铺编号" prop="shopNumber">
          <el-input v-model="form.shopNumber" placeholder="请输入店铺编号" />
        </el-form-item>
        <el-form-item label="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="商品编码" prop="productNumber">
          <el-input v-model="form.productNumber" placeholder="请输入商品编码" />
        </el-form-item>
        <el-form-item label="金蝶的物料编码" prop="jdMaterialCode">
          <el-input v-model="form.jdMaterialCode" placeholder="请输入金蝶的物料编码" />
        </el-form-item>
        <el-form-item label="款式编码" prop="styleNumber">
          <el-input v-model="form.styleNumber" placeholder="请输入款式编码" />
        </el-form-item>
        <el-form-item label="申请数量" prop="applyNumber">
          <el-input v-model="form.applyNumber" placeholder="请输入申请数量" />
        </el-form-item>
        <el-form-item label="单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="申请金额" prop="applyPrice">
          <el-input v-model="form.applyPrice" placeholder="请输入申请金额" />
        </el-form-item>
        <el-form-item label="颜色规格" prop="colorSku">
          <el-input v-model="form.colorSku" placeholder="请输入颜色规格" />
        </el-form-item>
        <el-form-item label="原订单下单金额" prop="originalOrderPrice">
          <el-input v-model="form.originalOrderPrice" placeholder="请输入原订单下单金额" />
        </el-form-item>
        <el-form-item label="最后确认时间（用于时间区段搜索）" prop="newOrderDate">
          <el-date-picker clearable v-model="form.newOrderDate" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择最后确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后确认时间" prop="lastConfirmationTime">
          <el-input v-model="form.lastConfirmationTime" placeholder="请输入最后确认时间" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="JstAfterSaleOrder" lang="ts">
import {
  // 02、引入后端接口
  listJstAfterSaleOrderByShopName2,
  getJstAfterSaleOrder,
  delJstAfterSaleOrder,
  addJstAfterSaleOrder,
  updateJstAfterSaleOrder
} from '@/api/jdAndJstSceneClassification/twoCSale/jstAfterSaleOrder';
import {
  JstAfterSaleOrderVO,
  JstAfterSaleOrderQuery,
  JstAfterSaleOrderForm
} from '@/api/jdAndJstSceneClassification/twoCSale/jstAfterSaleOrder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 定义日期范围
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const jstAfterSaleOrderList = ref<JstAfterSaleOrderVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const jstAfterSaleOrderFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: JstAfterSaleOrderForm = {
  id: undefined,
  afterSaleNumber: undefined,
  innerOrderNumber: undefined,
  onlineType: undefined,
  afterSaleCategory: undefined,
  onlineApplyMoney: undefined,
  onlineQuitFreight: undefined,
  discountOrDifference: undefined,
  sellerShouldQuitMoney: undefined,
  buyerShouldSupplementMoney: undefined,
  sellerShouldQuitFreight: undefined,
  actualQuitMoney: undefined,
  shopNumber: undefined,
  shopName: undefined,
  originalOrderType: undefined,
  afterSaleStatus: undefined,
  productNumber: undefined,
  jdMaterialCode: undefined,
  styleNumber: undefined,
  applyNumber: undefined,
  unitPrice: undefined,
  applyPrice: undefined,
  afterSaleType: undefined,
  colorSku: undefined,
  originalOrderPrice: undefined,
  newOrderDate: undefined,
  lastConfirmationTime: undefined
};
const data = reactive<PageData<JstAfterSaleOrderForm, JstAfterSaleOrderQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    afterSaleNumber: undefined,
    innerOrderNumber: undefined,
    onlineType: undefined,
    afterSaleCategory: undefined,
    onlineApplyMoney: undefined,
    onlineQuitFreight: undefined,
    discountOrDifference: undefined,
    sellerShouldQuitMoney: undefined,
    buyerShouldSupplementMoney: undefined,
    sellerShouldQuitFreight: undefined,
    actualQuitMoney: undefined,
    shopNumber: undefined,
    shopName: undefined,
    originalOrderType: undefined,
    afterSaleStatus: undefined,
    productNumber: undefined,
    jdMaterialCode: undefined,
    styleNumber: undefined,
    applyNumber: undefined,
    unitPrice: undefined,
    applyPrice: undefined,
    afterSaleType: undefined,
    colorSku: undefined,
    originalOrderPrice: undefined,
    newOrderDate: undefined,
    lastConfirmationTime: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: 'id不能为空', trigger: 'blur' }],
    afterSaleNumber: [{ required: true, message: '售后单号不能为空', trigger: 'blur' }],
    innerOrderNumber: [{ required: true, message: '内部订单号不能为空', trigger: 'blur' }],
    onlineType: [{ required: true, message: '线上类型不能为空', trigger: 'change' }],
    afterSaleCategory: [{ required: true, message: '售后分类不能为空', trigger: 'blur' }],
    onlineApplyMoney: [{ required: true, message: '线上申请金额不能为空', trigger: 'blur' }],
    onlineQuitFreight: [{ required: true, message: '线上退运费不能为空', trigger: 'blur' }],
    discountOrDifference: [{ required: true, message: '优惠或者差额不能为空', trigger: 'blur' }],
    sellerShouldQuitMoney: [{ required: true, message: '卖家应退金额不能为空', trigger: 'blur' }],
    buyerShouldSupplementMoney: [{ required: true, message: '买家应补金额不能为空', trigger: 'blur' }],
    sellerShouldQuitFreight: [{ required: true, message: '卖家应退运费不能为空', trigger: 'blur' }],
    actualQuitMoney: [{ required: true, message: '实际应退金额不能为空', trigger: 'blur' }],
    shopNumber: [{ required: true, message: '店铺编号不能为空', trigger: 'blur' }],
    shopName: [{ required: true, message: '店铺名称不能为空', trigger: 'blur' }],
    originalOrderType: [{ required: true, message: '原订单类型不能为空', trigger: 'change' }],
    afterSaleStatus: [{ required: true, message: '售后状态不能为空', trigger: 'change' }],
    productNumber: [{ required: true, message: '商品编码不能为空', trigger: 'blur' }],
    jdMaterialCode: [{ required: true, message: '金蝶的物料编码不能为空', trigger: 'blur' }],
    styleNumber: [{ required: true, message: '款式编码不能为空', trigger: 'blur' }],
    applyNumber: [{ required: true, message: '申请数量不能为空', trigger: 'blur' }],
    unitPrice: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
    applyPrice: [{ required: true, message: '申请金额不能为空', trigger: 'blur' }],
    afterSaleType: [{ required: true, message: '售后单类型不能为空', trigger: 'change' }],
    colorSku: [{ required: true, message: '颜色规格不能为空', trigger: 'blur' }],
    originalOrderPrice: [{ required: true, message: '原订单下单金额不能为空', trigger: 'blur' }],
    newOrderDate: [{ required: true, message: '最后确认时间（用于时间区段搜索）不能为空', trigger: 'blur' }],
    lastConfirmationTime: [{ required: true, message: '最后确认时间不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 03、更改原始的查询方法 查询聚水潭-售后单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listJstAfterSaleOrderByShopName2(proxy?.addDateRange(queryParams.value, dateRange.value));
  jstAfterSaleOrderList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  jstAfterSaleOrderFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: JstAfterSaleOrderVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加聚水潭-售后单';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: JstAfterSaleOrderVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getJstAfterSaleOrder(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改聚水潭-售后单';
};

/** 提交按钮 */
const submitForm = () => {
  jstAfterSaleOrderFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateJstAfterSaleOrder(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addJstAfterSaleOrder(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: JstAfterSaleOrderVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除聚水潭-售后单编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delJstAfterSaleOrder(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'twoCSale/jstAfterSaleOrder/export',
    {
      ...queryParams.value
    },
    `jstAfterSaleOrder_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
